<script setup>
import PageContainer from "@/components/PageContainer.vue"
import ChannelSelect from "@/views/article/components/ChannelSelect.vue";
import { Delete, Edit } from "@element-plus/icons-vue";
import { ref } from "vue";
import { artGetListService, artGetAllListService } from "@/api/article";
import { formatTime } from "@/utils/format";
import ArticleEdit from "@/views/article/components/ArticleEdit.vue";
const articleList = ref([])
const total = ref(0)
const loading = ref(false)
const title = ref('')

// 编辑逻辑
const onEditArticle = (row) => {
  title.value = "编辑文章"
  articleEditRef.value.open(row)

}
// 删除逻辑
const onDeleteArticle = (row) => {
  console.log(row);

}
const articleEditRef = ref()
// 添加逻辑
const onAddArticle = () => {
  title.value = "添加文章"
  articleEditRef.value.open({})
}

// 用户选择的分类
// 定义请求参数对象
const params = ref({
  pageNum: 1,
  pageSize: 5,
  cateId: null,
  state: null
})
// 获取文章
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data
  loading.value = false
}
getArticleList()
// 处理分页逻辑
const onSizeChange = (size) => {
  // 页面条数变化，则页数置1
  // console.log(size);
  params.value.pageNum = 1
  params.value.pageSize = size
  getArticleList()
}

const onCurrentChange = (page) => {
  // console.log(page);
  params.value.pageNum = page
  getArticleList()
}
// 获取总条数
const getAllArticleList = async () => {
  const res = await artGetAllListService()
  total.value = res.data
}
getAllArticleList()
// 搜索逻辑=>按照最新的条件,重新搜索,从第一页展示
const onSearch = () => {
  params.value.pageNum = 1
  getArticleList()
}
// 重置逻辑=>将搜索条件清空,从第一页开始
const onReset = () => {
  params.value.pageNum = 1
  params.value.cateId = null
  params.value.cateId = null
  getArticleList()
}

// 添加或者编辑草稿的回调

const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，最好渲染最后一页
    const lastPage = Math.ceil((total.value + 1) / params.value.pageSize)
    // 更新为最大页码数再渲染
    params.value.pageNum = lastPage
    getArticleList()
  } else {
    // 直接渲染当前页
    getArticleList()
  }
}

</script>

<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类:">
        <!-- label展示给用户看的,value收集起来提交给后台的 -->
        <!-- 在vue3中，v-model是:modelValue和@update:modelValue -->
        <ChannelSelect v-model="params.cateId"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态:">
        <el-select v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" align="center">
        <template #default="{ row }">
          <el-link type="primary" underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cateName" align="center"></el-table-column>
      <el-table-column label="发表时间" prop="pubDate" align="center">
        <template #default="{ row }">
          {{ formatTime(row.pubDate) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button circle plain :icon="Edit" type="primary" @click="onEditArticle(row)"></el-button>
          <el-button circle plain :icon="Delete" type="danger" @click="onDeleteArticle(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination class="page" v-model:current-page="params.pageNum" v-model:page-size="params.pageSize"
      :page-sizes="[2, 3, 5, 10]" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="onSizeChange" @current-change="onCurrentChange"
      style="margin-top: 20px;justify-content: end; margin-right: 10px;" />
    <!-- 添加编辑的抽屉 -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess" :title="title"></ArticleEdit>
  </PageContainer>
</template>

<style scoped>
:deep(.el-tooltip__trigger) {
  width: 250px;
}

:deep(.page .el-select .el-select__wrapper.el-tooltip__trigger.el-tooltip__trigger) {
  width: 140px;
}
</style>
